<template>
	<view>
		<view class="page-box">
			<view v-if="shopList.length > 0" class="shop-list">
			<u-checkbox-group>
				<view class="item" v-for="(item, index) in goodsList" :key="index">
					
					<view class="cart-title-icon select">
						<u-checkbox label-size="28rpx" v-model="item.checked" @change="changeGoodsStatus(index)"
							:label-disabled="false" shape="circle">
						</u-checkbox>
					</view>
					<view class="left">
						<image :src="item.cover" mode="aspectFill"></image>
					</view>
					<view class="right">
						<view class="title u-line-2">{{ item.name }}</view>
						<view class="price">
							￥{{ priceInt(item.price) }}
							<text class="decimal">.{{ priceDecimal(item.price) }}</text>
						</view>
						<view class="li-num-price">
							<view class="cartNum" @tap.stop="">
								<u-number-box v-model="item.number" :asyncChange="true" :min="1" @change="cartCountChange(index,item.number)" />
							</view>
						</view>
					</view>
				</view>
				</u-checkbox-group>
				
				<!-- 底部结算栏 -->
				<view class="footer flex bg-white">
					
					<u-checkbox-group class="normal">
						<u-checkbox label-size="28rpx" @change="changeAllSelect" id="checkAll" v-model="isSelectedAll"
							:label-disabled="false" shape="circle">
							<text class="normal">全选</text>
						</u-checkbox>
					</u-checkbox-group>
					
					<view class="total">
						共{{ totalNum(goodsList) }}件商品 合计:
						<text class="total-price">
							￥{{ priceInt(totalPrice(goodsList)) }}.
							<text class="decimal">{{ priceDecimal(totalPrice(goodsList)) }}</text>
						</text>
					</view>
					<view class="bottom">
						<view class="btn" @click="order()">结算</view>
					</view>
				</view>
				
			</view>

			<!-- 购物车空状态 -->
			<view class="cart-null bg-white m-b-20" v-else>
				<image class="img-null" src="/static/noCart.png" mode="aspectFill"></image>
				<view class="muted m-b-20">购物车暂无任何商品~</view>
				<view class="muted m-b-20"><u-button shape="circle" @click="goShoping()" size="medium">去逛逛</u-button></view>
				
			</view>

		</view>
	</view>
</template>

<script>
	import {getCart} from "@/api/api.js"
	import {mapState,mapGetters,mapMutations} from 'vuex';
	export default {
		data() {
			return {
				delPopup:false,
				goodsList: [],
				
				isSelectedAll: false,   //是否全选
			}
		},
		onLoad() {
		},
		onShow() {
			this.getData();
			console.log(this.shopList);
		},
		computed: {
			...mapState(['shopList']),
			// 价格小数
			priceDecimal() {
				return val => {
					if (val !== parseInt(val)) return val.slice(-2);
					else return '00';
				};
			},
			// 价格整数
			priceInt() {
				return val => {
					if (val !== parseInt(val)) return val.split('.')[0];
					else return val;
				};
			}
		},
		methods: {
			...mapMutations(['setShopList']),
			
			
			// 总价
			totalPrice(item) {
				let price = 0;
				item.map(val => {
					price += parseFloat(val.price);
				});
				return price.toFixed(2);
			},
			// 总件数
			totalNum(item) {
				let num = 0;
				item.map(val => {
					num += val.number;
				});
				return num;
			},
			
			
			async getData() {
				if(this.shopList.length<1){
					return;
				}
				console.log(this.shopList);
				// uni.showLoading({
				//     title: '加载中...'
				// });
				let data = {
					'shopList': JSON.stringify(this.shopList),
					// (this.shopList)
				};
				console.log(data);
				await getCart(data).then((res) => {
					uni.hideLoading();
					console.log(res);
					this.goodsList = res;
				}).catch(() => {
				
				})
			},
			
			
			// 全选商品
			changeAllSelect() {
				const status = this.isSelectedAll == true ? 0 : 1
				const cartIDs = this.goodsListActive.map(item => item.id)
				apiCartSelectChange({
					cart_id: cartIDs,
					selected: status
				}).then(res => {
					this.getCartList()
				})
			},
			
			
			//设置默认数据
			add(){
				const defaultData = [{
					id: 451,
					num: 2
				}];
				this.setShopList({ shopList: defaultData});
				console.log(this.shopList);
				this.getData();
				console.log(this.goodsList);
			},
			
			//删除数据
			delShop(index){
				let newData = this.shopList.splice(index, 1);
				this.setShopList({ shopList:newData });
				this.getData();
			},

			//修改数量
			cartCountChange(index,num){
				console.log(this.goodsList);
				console.log('num',num);
				console.log(index);
			// cartCountChange(index,num){
				console.log(this.goodsList[index].id);
				setTimeout(() => {
				console.log(this.goodsList[index].number);
				},500)
				// let newData = this.shopList;
				// newData[index].num = this.goodsList[index].number;
				// this.setShopList({ shopList:newData });
				// this.getData();
			},
			
			order(){
				console.log(this.shopList)
				uni.navigateTo({
					url: "/pages/order/send"
				})
			},
			goShoping(){
				console.log(this.shopList)
				uni.switchTab({
					url: "/pages/index/index"
				})
			},
			

		}
	}
</script>

<style lang="scss" scoped>
.shop-list {
	background-color: #ffffff;
	border-radius: 20rpx;
	box-sizing: border-box;
	padding: 20rpx;
	font-size: 28rpx;
	display:flex;
	flex-direction: column;
	
	
	.item {
		display: flex;
		align-items: center;
		width: 100%;
		padding-bottom: 10rpx;
		border-bottom: 1rpx solid #eee;

		.left {
			margin-right: 20rpx;
			image {
				width: 160rpx;
				height: 160rpx;
				border-radius: 10rpx;
				margin-right: 20rpx;
			}
		}
		.content {
			.title {
				font-size: 28rpx;
				line-height: 50rpx;
			}
			.type {
				margin: 10rpx 0;
				font-size: 24rpx;
				color: $u-tips-color;
			}
			.delivery-time {
				color: #e5d001;
				font-size: 24rpx;
			}
		}
		.right {
			flex:1;
			margin-left: 10rpx;
			padding-top: 20rpx;
			text-align: left;
			.decimal {
				font-size: 24rpx;
				margin-top: 4rpx;
			}
			.number {
				color: $u-tips-color;
				font-size: 24rpx;
			}
		}
	}
	.total {
		text-align: right;
		font-size: 24rpx;
		.total-price {
			font-size: 32rpx;
		}
	}
	.bottom {
		
		display: flex;
		padding: 0 10rpx;
		justify-content: space-between;
		align-items: center;
		.btn {
			line-height: 60rpx;
			width: 160rpx;
			border-radius: 60rpx;
			border: 2rpx solid $u-border-color;
			font-size: 26rpx;
			text-align: center;
			color: $u-info-dark;
		}
		.evaluate {
			color: $u-warning-dark;
			border-color: $u-warning-dark;
		}
	}

}
	.footer {
		left: 0;
		bottom: 0;
		width: 100%;
		z-index: 20;
		height: 100rpx;
		margin-bottom: 100rpx;


		position: fixed;
		padding: 0 24rpx;
		box-sizing: border-box;
		background-color: #FFFFFF;
		box-shadow: 0px 0px 12px rgba(0, 0, 0, 0.1);
		display: flex;
		align-items: center;
		justify-content: space-between;

		.all-price {
			text-align: right;
			flex: 1;
			align-items: center;
			justify-content: flex-end;
			padding-right: 20rpx;
		}

		.right-btn {
			padding: 13rpx 45rpx;
			background-color: #ff2c3c;
			color: #ffffff;
		}
	}
.cart-null{
	text-align: center;
}
</style>